<!-- 选择医保支付还是现金支付弹窗 -->
<template>
	<transition name="el-fade-in" v-if="chosePayWayShow">
		<div>
			<div class="payByCardErrorBox">
				<div class="topBox">
					<div class="topBoxLeft"></div>
					<div class="topBoxRight el-icon-close" @click="hidBox()">
					</div>
				</div>
				<div v-if="code=='mzjf'" class="centerBox">
					请选择支付方式
				</div>
				<div class="payByCardErrorBoxBottomBox">
					<el-button class="payByCardErrorBoxBottomBoxBtn BtnTextHome" type="primary" @click="selfPay()">自费
					</el-button>
<!-- 					<el-button class="payByCardErrorBoxBottomBoxBtn BtnTextGreen"
						style="width: 180px !important;background-color: #888888;" type="success" @click="ybPay()">
						医保(勿点)
					</el-button> -->

					<el-button class="payByCardErrorBoxBottomBoxBtn BtnTextGreen"
						 type="success" @click="ybPay()">
						医保
					</el-button>
				</div>
			</div>
			<div class="yiny"></div>
		</div>
	</transition>
</template>

<script>
	import {
		MessageBox,
		Loading
	} from "element-ui";
	import {
		patientInfoPack,
		patientInfoParse,
		preSettlePack,
		preSettleParse,
		settlePack,
		settleParse
	} from "@api/apiBox/MedInsTradeYn"
	import {
		preSettleBatch
	} from "@api/apiBox/mzjf"
	import {
		searchPayStatus
	} from "@api/apiBox/pay"
	import {
		CallMedInsYn
	} from '@/api/tools/MedInsTradeYn';
	import {
		unlockReg
	} from "@api/apiBox/mzyyAndMzgh"
	export default {
		name: "PaySuccess",
		data() {
			return {
				YB: new CallMedInsYn(),
				timer: '', //定时器,
				cashAmount: ''
			}
		},
		props: {
			chosePayWayShow: {
				default: false,
				type: Boolean
			},
			//类型
			code: {
				default: '',
				type: String
			}
		},
		methods: {
			selfPay() {
				this.$store.dispatch("user/updateisYbPay", false);
				this.$emit('closeChosePayWayShow')
			},
			ybPay() {
				this.$store.dispatch("user/updateisYbPay", true);
				this.$emit('closeChosePayWayShow')
				this.preSettle();
			},
			//医保预结算
			async preSettle() {
				let payinfo = {
					data: {
						patientId: this.$store.state.user.userinfo.patientId, //患者ID
						termId: this.$store.state.terminfo.terminfo.termId, //终端ID
						totalFee: this.$store.state.recipel.records.amount, //总费用
						orderType: 2, //1: 挂号 订单   2: 缴费订单 3: 处方流转订单  4: 处方流转快递订单 5:  预交金充值订单
						selfFee: this.$store.state.recipel.records.amount,
						payWay: 1, //支付渠道 1 微信 2 支付宝 3 银行卡
						billNo: this.$store.state.recipel.records.receiptNo,
						zycs: this.$store.state.user.userinfo.admissionTimes,
						hisVisitNo: this.$store.state.recipel.records.billNo,
						receiptNos: this.$store.state.recipel.records.receiptNos,
						patientName: this.$store.state.user.userinfo.patientName, //患者姓名
						idNo: this.$store.state.appointment.cardNo, //身份证号码
						patientSex: this.$getTime.IdCard(this.$store.state.user.userinfo.idNo, 2)
					},
					that: this
				}
				this.payinfo = payinfo;
				//门诊结算
				if (this.code == 'mzjf') {
					//输出订单消息
					this.$store.dispatch("user/updatechargefee", this.$store.state.recipel.records.amount); //支付金额  总费用
					console.log(JSON.stringify(payinfo.data))

					let packData = {
						data: {
							cardType: 9
						},
						that: this
					}

					//医保刷卡前获取参数
					const patientInfoPackRes = await patientInfoPack(packData);

					//调用医保读卡
					const dkRes = await this.YB.Trade('BMZXX010', patientInfoPackRes.data);


					let patientInfoParseData = {
						data: JSON.parse(dkRes),
						that: this
					}
					//医保解析
					const patientInfoParseRes = await patientInfoParse(patientInfoParseData);

					let IPatientId = patientInfoParseRes.data.patid;
					payinfo.data.IPatientId = IPatientId;
					this.payinfo = payinfo;

					let res = "";
					//预结算 获取二维码 缴费订单
					res = await preSettlePack(payinfo);
					console.log('医保门诊结算preSettlePack：' + res.data)
					//存储缴费二维码信息 医保
					this.$store.dispatch("recipel/updateErCodeYbInfo", res.data);


					console.log('开始唤醒医保组件')
					//唤醒医保组件
					var preSettleRtn = await this.YB.Trade('BMZJF001', res.data);
					//获取自费金额
					let preSettleParseData = {
						data: JSON.parse(preSettleRtn),
						that: this
					}
					let selfRes = await preSettleParse(preSettleParseData);

					//存储hisVisitNo
					payinfo.data.IVisitNo = selfRes.data.insVisitNo;
					payinfo.data.selfFee = selfRes.data.cashAmount;
					this.payinfo = payinfo;

					console.log('医保门诊结算 获取自费金额：' + selfRes)
					//获取自费金额
					let cashAmount = selfRes.data.cashAmount;
					this.cashAmount = cashAmount;
					//获取医保支付金额
					let fundAmount = selfRes.data.fundAmount;
					//医保余额
					let accountAmount1 = selfRes.data.accountAmount1;
					//总金额
					let allAmount = selfRes.data.amount;
					this.$store.dispatch("user/updateIPatientId", IPatientId); //医保个人编号
					this.$store.dispatch("user/updateIVisitNo", selfRes.data.insVisitNo); //医保收据号
					this.$store.dispatch("user/updateUserAllPay", allAmount); //医保+自费总金额
					this.$store.dispatch("user/updateuserpay", cashAmount); //自费金额
					this.$store.dispatch("user/updateybpay", fundAmount); //医保支付金额
					this.$store.dispatch("user/updateybzhye", accountAmount1); //医保余额
					let _this = this;
					//如果自费金额不为0  则再走一遍支付流程
					if (cashAmount != 0) {
						MessageBox.alert("医保可支付:" + fundAmount + '元,还需自费金额：' + cashAmount + '元', {
							confirmButtonText: "确定",
							callback: action => {
								MessageBox.close();
								_this.$emit('closeChosePayWayShow')
							}
						});
					} else {
						console.log('医保门诊结算 自费为0走一遍以前流程')
						let preSettleBatchRes = await preSettleBatch(payinfo);
						console.log('医保门诊结算 自费为0走一遍以前流程 预结算', preSettleBatchRes)
						//存储缴费二维码信息
						this.$store.dispatch("recipel/updateErCodeInfo", preSettleBatchRes.data);
						this.payinfo.orderId = preSettleBatchRes.data.orderId;
						//查询订单状态
						this.searchPayStatusEvent();
					}
				}
			},
			async searchPayStatusEvent() {
				let _this = this;
				//查询订单状态
				let stausData = {
					data: {
						orderId: _this.$store.state.recipel.erCodeInfo.orderId,
						isCallback: 0 // 1-自动通知HIS支付结果 0-不自动通知HIS支付结果 自助机传0  H5传1
					}
				};
				const result = await searchPayStatus(stausData).catch(() => {
					//获取支付状态报错 500 调用his接口 仅限预约挂号
					_this.unlockRegMethod();
				});
				console.log('医保门诊结算 自费0 查询订单状态：' + result)
				let res = result.data.status;
				//成功payOrderId 0为全额医保支付
				if (res == 2 || res == 0) {
					if (_this.code == 'mzyy' || _this.code == "mzgh") {
						result.data.payWay = '微信支付';
						console.log(result.data, 'result');
						this.$store.dispatch("payrecord/updatepayinfo", result.data);
					} else if (_this.code == 'mzjf' || _this.code == "yjjcz") {
						this.$store.dispatch("recipel/updateBillInfo", result.data);
						let payinfo = {
							payWay: '微信支付',
							rcptNo: '111',
							orderId: _this.$store.state.recipel.erCodeInfo.payOrderId
						}
						this.$store.dispatch("payrecord/updatepayinfo", payinfo);
					}
					clearInterval(_this.timer)
					_this.payinfo.data.selfFee = _this.cashAmount;
					_this.payinfo.data.orderId = _this.$store.state.recipel.erCodeInfo.orderId;
					const settlePackRes = await settlePack(_this.payinfo);
					console.log("医保结算入参：");
					console.log(settlePackRes.data);
					//唤醒医保组件
					//唤醒医保组件
					var settleRtn = await this.YB.Trade('BMZJF002', settlePackRes.data);
					console.log("调用医保机器返回：",settleRtn);
					let settleParseItemData = typeof settleRtn == "string" ? JSON.parse(settleRtn) : {};
					console.log("调用医保机器返回解析：",settleParseItemData);
					//获取自费金额
					let settleParseData = {
						data: settleParseItemData,
						that: this
					}
					let settleParseRes = await settleParse(settleParseData);
					console.log("医保结算返回：",settleParseRes);
					let records = _this.$store.state.recipel.records;
					records.paidOrdersSuccessByMzjfRes = settleParseRes.data;
					_this.$store.dispatch("recipel/updateRecordsInfo", records); //存储缴费订单信息
					if(res == 0){
						setTimeout(() => {
							_this.$emit("payRes", '1');
						}, 100);
					}else{
						setTimeout(() => {
							_this.$emit("payRes", 'success');
						}, 2000);
					}
				}
				//失败
				if (res == 3) {
					clearInterval(_this.timer)
					_this.$emit("payRes", 'fail');
				}
				//还没有支付
				if (res == 1) {
					console.log('还没有支付!', _this.payWxBoxIsSHow);
					if (!_this.isEndPay) {
						setTimeout(() => _this.searchPayStatusEvent(), 3000);
					} else {
						clearInterval(_this.timer)
					}
				}
			},
			async unlockRegMethod() {
				if (this.code == 'mzyy' || this.code == "mzgh") {
					let unlockRegData = {
						data: {
							regId: this.$store.state.appointment.appointmentInfo.regId,
							queueNo: this.$store.state.appointment.appointmentInfo.queueNo,
							regDate: this.$store.state.appointment.date //预约日期
						},
						that: this
					}
				}
				const unlockRegMethodRes = await unlockReg(unlockRegData);
				console.log('解锁号源成功！')
			},
			hidBox() {
				this.$confirm('关闭窗口将返回到首页，确定取消支付?', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning',
				}).then(() => {
					this.goBackHome();
				}).catch(() => {

				});
			}
		},
		mounted() {},
	}
</script>

<style lang="scss" scoped>
	@media (min-height: 0px) and (max-height: 1910px) {
		.payByCardErrorBox {
			position: fixed;
			width: 38.125rem;
			height: 21.375rem;
			background: #FFFFFF;
			bottom: calc(50% - 10.68725rem);
			left: calc(50% - 19.0625rem);
			z-index: 3;
			text-align: center;
			box-shadow: 0px 1px 27px 0px rgba(42, 103, 177, 0.09);
			border-radius: 6px;
		}

		.centerBox {
			font-size: 2.3vw;
		}

		.payByCardErrorBoxBottomBox {
			margin-top: 3.375rem;
			display: flex;
			justify-content: center;

			.payByCardErrorBoxBottomBoxBtn {
				margin: 0px 2rem !important;
			}
		}


	}

	@media (min-height: 1919px) {

		.payByCardErrorBox {
			position: fixed;
			background: #FFFFFF;
			width: 38.125rem;
			height: 21.375rem;
			bottom: 350px;
			left: calc(50% - 19.0625rem);
			z-index: 3;
			text-align: center;
			box-shadow: 0px 1px 27px 0px rgba(42, 103, 177, 0.09);
			border-radius: 6px;
		}



		.centerBox {
			font-size: 2.4vw;
		}

		.payByCardErrorBoxBottomBox {
			margin-top: 3.375rem;
			display: flex;
			justify-content: center;

			.payByCardErrorBoxBottomBoxBtn {
				margin: 0px 2rem !important;
			}
		}

	}







	.itemIitle {
		width: 100%;
		text-align: center;
		font-size: 1.8625rem;
		margin-top: 1.125rem;
	}

	.successBox {
		width: 150px;
		height: 139px;
		margin-top: 50px;
	}

	.cardMar {
		margin-top: 25px;
	}


	.topBox {
		width: 100%;
		height: 50px;
		margin-top: 21px;
		margin-left: 20px;
	}

	.topBoxLeft {
		float: left;
		height: 100%;
		font-size: 32px;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #57A8FF;
	}

	.topBoxRight {
		width: 50px;
		height: 50px;
		background: #57A8FF;
		border-radius: 6px;
		position: absolute;
		right: 0px;
		top: 0px;
		color: #FFF0FF;
		font-size: 46px;
	}
</style>
